package com.ruoyi.generator.config;
import cn.hutool.db.handler.ValueListHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.ruoyi.generator.domain.DataBaseConfig;
import com.ruoyi.generator.mapper.DataBaseConfigMapper;
import org.apache.calcite.adapter.jdbc.JdbcSchema;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.SchemaPlus;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.*;
import java.util.*;

/**
 *
 *      try {
 *             List<List<Object>> listList = SqlExecutor.query(calciteConnection,
 *                     "select * from NoStopVehCheckSystem.cus_check_point",
 *                     new ValueListHandler());
 *             for (List<Object> objects : listList) {
 *                 System.out.println("objects = " + objects);
 *             }
 *
 *
 *         } catch (SQLException e) {
 *             e.printStackTrace();
 *             throw new RuntimeException(e);
 *         }
 *
 *
 */
@Configuration
public class MultiSourceConfig implements SmartInitializingSingleton {

    @Autowired
    private DataBaseConfigMapper dataBaseConfigMapper;

    private static CalciteConnection calciteConnection;



    private void init(){

        List<DataBaseConfig> dataBaseConfigs = dataBaseConfigMapper.selectGenTableList(null);
        Properties config = new Properties();
        config.put("caseSensitive" , "false");
        try {
            Connection connection = DriverManager.getConnection("jdbc:calcite:" , config);
            calciteConnection = connection.unwrap(CalciteConnection.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        for (DataBaseConfig dataBaseConfig : dataBaseConfigs) {
            SchemaPlus rootSchema = calciteConnection.getRootSchema();
            DataSource dataSource = JdbcSchema.dataSource(dataBaseConfig.getJdbcUrl(),
                    dataBaseConfig.getJdbcDriver(),
                    dataBaseConfig.getJdbcUsername(),
                    dataBaseConfig.getJdbcPassword());

            Schema schema1 = JdbcSchema.create(rootSchema, dataBaseConfig.getDatabaseName(),dataSource,null,null);
            rootSchema.add(dataBaseConfig.getDatabaseName(), schema1);
        }
    }
    @Override
    public void afterSingletonsInstantiated() {
        init();

        try {
           List<List<Object>> listList = SqlExecutor.query(calciteConnection,
                                       "select column_name, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, \n" +
                                               "(case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type\n" +
                                               "from test.columns where table_schema = ? and table_name = ?" +
                                               "order by ordinal_position", new ValueListHandler(),"test","employee");
              for (List<Object> objects : listList) {
                  System.out.println("objects = " + objects);
              }


          } catch (SQLException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
          }

    }

    public static CalciteConnection getCalciteConnection() {
        return calciteConnection;
    }
}
